package share.controller;

import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import share.service.GrossRevenueReportService;
import share.service.ShareService;
import share.service.model.GrossRevenueReportModel;
import share.service.model.PageModel;
import share.service.model.ShareModel;
import share.viewobject.GrossRevenueReportVO;
import share.viewobject.ShareVO;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
public class ShareController {

    @Autowired
    private ShareService shareService;

    @Autowired
    private GrossRevenueReportService grossRevenueReportService;

    @GetMapping("addShareHtml")
    public String addShare() {
        return "redirect:listByPage";
    }

    @PostMapping("addShare")
    public String addShare(@RequestParam(name = "shareCode") String shareCode,
                           @RequestParam(name = "shareName") String shareName,
                           @RequestParam(name = "currentPage", defaultValue = "1", required = false) Integer currentPage,
                           @RequestParam(name = "pageSize", defaultValue = "5", required = false) Integer pageSize,
                           Model model) {
        ShareModel share = new ShareModel();
        share.setShareCode(shareCode);
        share.setShareName(shareName);
        share.setCreateDate(new Date());
        shareService.addShare(share);

        return "redirect:listByPage";
    }

    @GetMapping("editShare")
    public String editShare(@RequestParam(name = "id") Integer id, Model model) {
        ShareModel shareModel = shareService.getShareById(id);
        ShareVO shareVO = new ShareVO();
        BeanUtils.copyProperties(shareModel, shareVO);
        model.addAttribute("share", shareVO);
        return "share/editShare";
    }

    @PostMapping("editShare")
    public String editShare(@RequestParam(name = "id") Integer id,
                           @RequestParam(name = "shareCode") String shareCode,
                           @RequestParam(name = "shareName") String shareName) {
        ShareModel share = new ShareModel();
        share.setId(id);
        share.setShareCode(shareCode);
        share.setShareName(shareName);
        shareService.editShare(share);

        return "redirect:listByPage";
    }

    @GetMapping("listByPage")
    public String listShareByPage(@RequestParam(name = "currentPage", defaultValue = "1", required = false) Integer currentPage,
                                  @RequestParam(name = "pageSize", defaultValue = "5", required = false) Integer pageSize,
                                  Model model) {
        PageModel<ShareModel> pageModel = shareService.findByPage(currentPage, pageSize);
        List<ShareVO> shareVOList = pageModel.getList().stream().map(shareModel -> {
            ShareVO shareVO = new ShareVO();
            BeanUtils.copyProperties(shareModel, shareVO);
            shareVO.setCreateDate(new DateTime(shareModel.getCreateDate()).toString("yyyy-MM-dd"));
            return shareVO;
        }).collect(Collectors.toList());

        PageModel<ShareVO> shares = new PageModel<>();
        shares.setTotalPage(pageModel.getTotalPage());
        shares.setTotalCount(pageModel.getTotalCount());
        shares.setPageSize(pageModel.getPageSize());
        shares.setCurrPage(pageModel.getCurrPage());
        shares.setList(shareVOList);
        model.addAttribute("shares", shares);
        return "share/addShare";
    }

    @GetMapping("/delete")
    public String deleteShare(@RequestParam(name = "id") Integer id) {
        shareService.deleteShare(id);
        return "redirect:listByPage";
    }

    @GetMapping("viewShare")
    public String viewShare(@RequestParam(name = "id") Integer id, Model model) {
        ShareModel shareModel = shareService.getShareById(id);
        ShareVO shareVO = new ShareVO();
        BeanUtils.copyProperties(shareModel, shareVO);
        model.addAttribute("share", shareVO);

        GrossRevenueReportModel grrModel = grossRevenueReportService.selectByShareId(id);
        GrossRevenueReportVO gross = fromGrrModel(grrModel);
        model.addAttribute("gross", gross);

        return "share/viewShare";
    }

    private GrossRevenueReportVO fromGrrModel(GrossRevenueReportModel grrModel) {
        if (grrModel == null) {
            return null;
        }
        GrossRevenueReportVO vo = new GrossRevenueReportVO();
        vo.getDateList().add(grrModel.getRevenueReportDate9());
        vo.getDateList().add(grrModel.getRevenueReportDate8());
        vo.getDateList().add(grrModel.getRevenueReportDate7());
        vo.getDateList().add(grrModel.getRevenueReportDate6());
        vo.getDateList().add(grrModel.getRevenueReportDate5());
        vo.getDateList().add(grrModel.getRevenueReportDate4());
        vo.getDateList().add(grrModel.getRevenueReportDate3());
        vo.getDateList().add(grrModel.getRevenueReportDate2());
        vo.getDateList().add(grrModel.getRevenueReportDate1());

        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport9()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport8()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport7()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport6()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport5()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport4()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport3()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport2()));
        vo.getDoubleList().add(Double.valueOf(grrModel.getRevenueReport1()));
        return vo;
    }


}
